#include <pppmst.h>
#include <translate.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int 
main(int argc, char *argv[]) {
    int size_vertices = 0;
    timeval tim;
    double t1, t2;
    PPPWGraph G; Graph_t BG;
    int opt;
    std::string infile = "";

    while ((opt = getopt(argc, argv, "s:i:")) != -1) {
        switch(opt) {
            case 's':
                size_vertices = atoi(optarg);
                break;
            case 'i':
                infile += optarg;
                break;
            default:
                fprintf(stderr, "Usage: %s [-s {size of vertices}] [-i {input file name}]", argv[0]);
                exit(EXIT_FAILURE);
        }
    }

    if (size_vertices == 0 || infile == "") {
        fprintf(stderr, "Usage: %s [-s {size of vertices}] [-i {input file name}]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    std::cout << "Initializing...\n";
    buildGraph(infile, &G, size_vertices);
    buildBoost(infile, &BG, size_vertices);
    std::cout << "Initializing done...\n";

    
    std::vector < graph_traits < Graph_t >::vertex_descriptor >
        p(num_vertices(BG));


    gettimeofday(&tim, NULL);
    t1=tim.tv_sec+(tim.tv_usec/1000000.0);	
    prim_minimum_spanning_tree(BG, &p[0]);
    gettimeofday(&tim, NULL);
    t2=tim.tv_sec+(tim.tv_usec/1000000.0);	
    printf("%.6lf seconds elapsed\n", t2-t1);


    WorkQueue * Q = new WorkQueue();
    Q->setNumThreads(2);
    Q->initThreads();

    pppmst * mst = new pppmst(Q, &G, size_vertices);
    Q->addTask(mst);
    Q->wait_for_threads();

#if 1     
    for (int i = 0 ; i < size_vertices ; i++) {
        std::cout << i << " ----> " << p[i] << std::endl;
    }
#endif
    
#if 0 
    WorkQueue * Q = new WorkQueue();
    Q->setNumThreads(2);
    Q->initThreads();

    std::cout << "Main going to sleep\n";
    sleep(15);
#endif
    return EXIT_SUCCESS;
}
